package com.zzyl.nursing.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.nursing.domain.Floor;
import com.zzyl.nursing.domain.Room;
import com.zzyl.nursing.domain.RoomType;
import com.zzyl.nursing.mapper.RoomMapper;
import com.zzyl.nursing.service.IFloorService;
import com.zzyl.nursing.service.IRoomService;
import com.zzyl.nursing.service.IRoomTypeService;
import com.zzyl.nursing.vo.RoomVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 房间Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-26
 */
@Service
public class RoomServiceImpl extends ServiceImpl<RoomMapper, Room> implements IRoomService {
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private IFloorService floorService;
    @Autowired
    private IRoomTypeService roomTypeService;

    /**
     * 查询房间
     *
     * @param id 房间主键
     * @return 房间
     */
    @Override
    public Room selectRoomById(Long id) {
        return getById(id);
    }

    /**
     * 查询房间列表
     *
     * @param room 房间
     * @return 房间
     */
    @Override
    public List<Room> selectRoomList(Room room) {
        return roomMapper.selectRoomList(room);
    }

    /**
     * 新增房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int insertRoom(Room room) {
        return save(room) ? 1 : 0;
    }

    /**
     * 修改房间
     *
     * @param room 房间
     * @return 结果
     */
    @Override
    public int updateRoom(Room room) {
        return updateById(room) ? 1 : 0;
    }

    /**
     * 批量删除房间
     *
     * @param ids 需要删除的房间主键
     * @return 结果
     */
    @Override
    public int deleteRoomByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 根据楼层 id 获取房间视图对象列表
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsByFloorId(Long floorId) {
        return roomMapper.selectByFloorId(floorId);
    }


    /**
     * 获取所有房间（负责老人）
     *
     * @param floorId
     * @return
     */
    @Override
    public List<RoomVo> getRoomsWithNurByFloorId(Long floorId) {
        return roomMapper.selectByFloorIdWithNur(floorId);
    }

    /**
     * 根据房间id查询房间数据(楼层、房间、价格)
     *
     * @param id
     * @return
     */
    @Override
    public RoomVo selectById(Integer id) {
        RoomVo vo = new RoomVo();
        //查询房间基本信息
        Room room = getById(id);
        BeanUtils.copyProperties(room, vo);
        vo.setRoomId(String.valueOf(room.getId()));
        //查询房间对应楼层的基本信息
        Floor floor = this.floorService.getById(room.getFloorId());
        vo.setFloorName(floor.getName());
        vo.setFloorId(String.valueOf(floor.getId()));
        //查询房间对应的房型的价格
        RoomType roomType = this.roomTypeService.lambdaQuery()
                .eq(RoomType::getName, room.getTypeName())
                .one();
        try {
            vo.setPrice(String.valueOf(roomType.getPrice()));
        } catch (Exception e) {
            throw new BaseException("目前这个房间正在维护中,请选择其他的房间吧~~");
        }
        return vo;
    }
}
